package com.ljz.dao.impl;

import com.ljz.dao.SearchKeyDao;
import com.ljz.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SearchKeyDaoImpl implements SearchKeyDao {
    @Override
    public List<String> searchKey(String key) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<String> list = new ArrayList<>();
        try{
            conn = DBUtil.getConn();
            String sql = "select keyname from search_key where keyname like ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "%"+key+"%");
            rs = ps.executeQuery();

            while(rs.next()){
                list.add(rs.getString("keyname"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.closeAll(conn,ps,rs);
        }
        return list;
    }
}
